<?php
	session_start();
	if(!isset($_SESSION['id'])){
		header('Location:login.php');
	}
    $username=$_SESSION['username'];//得到用户名

    $id=$_POST['id'];

    $type=$_POST['type'];//得到类型名称
	$jiekou_id=$_POST['publish_jiqi'];//得到测试机器码的备注
	//echo $jiekou_id;exit;
	date_default_timezone_set('prc');
    $uploadtime=date('Y-m-d H:i:s',time());//得到时间
	
	//取得shenji表的版本号(更新type表中的版本号)
	$connection=mysql_connect("localhost","root","");
	mysql_select_db("shenji",$connection);
	mysql_query("set names utf8");
	$sql="select * from type where name='".$type."'";
	$result=mysql_query($sql);
	$row=mysql_fetch_assoc($result);
	$id2=$row['typeid'];//type表中的id
	$version=$row['last_version'];
	
	$relversion=$row['last_version']+1;
	
	if ($_FILES["file"]["error"] > 0){
		echo "Error: " . $_FILES["file"]["error"] . "<br />";
	}else{
	   //是为覆盖文件
		if($id!=null){
			$sql2="select * from shenji where id=$id";
			$result2=mysql_query($sql2);
			$row2=mysql_fetch_array($result2);
			$ver=$row2['version'];
			//$type=$row2['type'];
			$txt = sprintf("%1\$.2f",$ver*0.01);
			$relfilename=$txt."_update.tar.gz";
		    #找到需要删除的文件
			$filename ="/var/www/html/upload/upload/".$type."/".$relfilename;
			if( is_file( $filename ) ){
				unlink($filename);
			}
		    //更新的话只是换包,不需要更新数据库
		    move_uploaded_file($_FILES["file"]["tmp_name"],"/var/www/html/upload/upload/".$type."/".$relfilename);
			//更新数据库
			$sql="update shenji set jiekou_id='$jiekou_id' where id=$id";
			mysql_query($sql);
			
		}else{
			//这个是添加
			$txt = sprintf("%1\$.2f",$relversion*0.01);
			$relfilename=$txt."_update.tar.gz";
			//echo $relfilename;exit;
		    move_uploaded_file($_FILES["file"]["tmp_name"],"/var/www/html/upload/upload/".$type."/".$relfilename);
		   if($type!=null){
		   //添加数据库
			$sql3="insert into shenji(type,version,username,uploadtime,move,filename,publish_jiqi,jiekou_id) value('$type','$relversion','$username','$uploadtime','$relversion','$relfilename',1,'$jiekou_id')";
		    mysql_query($sql3);
			}
		    //更新type表中的版本号
		    $sql4="update type set last_version=$relversion where typeid=$id2";
		    mysql_query($sql4);
			
			//日志信息
			#$record=$relusername."上传一个".$type."升级包  版本号为:".$relversion;
			# $sql4="insert into logging(record) value('$record')";
			#mysql_query($sql4);
	  }
	
   }
		echo "<script>location='select_shenji.php';</script>";
				
?>







